//
//  MineGalleryCollectionViewCell.swift
//  MT
//
//  Created by M on 2019/12/29.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory
import Kingfisher

class MineGalleryCollectionViewCell: UICollectionViewCell {
    
    var model: UserMediaListModel? {
        didSet {
            guard let model = model else {
                progressView.isHidden = true
                if status == .unselected {
                    imageView.image = UIImage(named: "icon_add_2")
                } else if status == .unselected_highlighted {
                    imageView.image = UIImage(named: "icon_add_1")
                }
                return
            }
            /// 设置图片
            if let asset = model.localAsset {
                requestPhoto(with: asset)
            } else if let coverImg = model.coverImg {
                imageView.kf.setImage(with: URL(string: coverImg), placeholder: UIImage(named: "placeholder-rectangle"))
            } else if let img = model.href {
                imageView.kf.setImage(with: URL(string: img), placeholder: UIImage(named: "placeholder-rectangle"))
            }
             
            progressView.isHidden = model.uploadStatus == .none || model.uploadStatus == .finished || model.uploadStatus == .fail
            progressView.progress = CGFloat(model.uploadProgress)
            
        }
    }
    
    var isPortraitTagLabelHidden: Bool = true {
        didSet {
            portraitTagLabel.isHidden = isPortraitTagLabelHidden
        }
    }
    
    /// 状态
    @objc
    enum Status: Int {
        case selected = 0
        case unselected_highlighted
        case unselected
    }
    var status: Status = .unselected
    
    /// 图片
    lazy var imageView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFill
        imgView.layer.cornerRadius = 4
        imgView.clipsToBounds = true
        return imgView
    }()
    
    /// 头像标签
    lazy var portraitTagLabel: UILabel = {
        let label = UILabel()
        label.text = "头像"
        label.font = PingFangSCRegularFont(11)
        label.textColor = UIColor.common.x161616
        label.textAlignment = .center
        label.backgroundColor = UIColor.white.withAlphaComponent(0.5)
        label.clipsToBounds = true
        label.layer.cornerRadius = 5
        label.isHidden = true
        return label
    }()
    
    /// 进度 View
    lazy var progressView: MTProgressView = {
        let pv = MTProgressView(frame: self.contentView.bounds, text: "正在上传")
        pv.isHidden = true
        return pv
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initializeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: ------------------------------------ Overrides ----------------------------------------
extension MineGalleryCollectionViewCell {
 
    override func prepareForReuse() {
        super.prepareForReuse()
        imageView.kf.cancelDownloadTask()
        imageView.image = nil
    }
    
}

// MARK: ------------------------------------ 初始化 ----------------------------------------
extension MineGalleryCollectionViewCell {
    
    private func initializeUI() {
        contentView.addSubview(imageView)
        contentView.addSubview(portraitTagLabel)
        contentView.addSubview(progressView)
        imageView.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
        portraitTagLabel.snp.makeConstraints { (make) in
            make.right.bottom.equalTo(contentView).offset(-3)
            make.width.equalTo(30)
            make.height.equalTo(16)
        }
    }
    
}

// MARK: ------------------ 图片读取 ----------------------
extension MineGalleryCollectionViewCell {
    
    private func requestPhoto(with asset: MTAsset?) {
        guard let asset = asset else { return }
        MTAssetLibrary.default.requestImage(withAsset: asset.asset, photoWidth: ScreenWidth / 4, progressHandler: nil, errorHandler: nil) { [weak self] (image, info, isDegraded) in
            guard let weakself = self else { return }
            /// 设置图片
            weakself.imageView.image = image
        }
    }
    
}
